/*
 * Copyright 2008 Lamen Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
*/

package org.lamen.sandbox;

import java.util.Date;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

/**
 * The event handler for the TimeServer <code>MINA</code> example
 * 
 * <code>http://mina.apache.org/mina-v20-quick-start-guide.html</code>
 * 
 * @author danielfireman
 * 
 * @date Nov 28, 2008
 */

public class TimeServerHandler extends IoHandlerAdapter {

  public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
    cause.printStackTrace();
  }

  public void messageReceived(IoSession session, Object message) throws Exception {
    String str = message.toString();
    if (str.trim().equalsIgnoreCase("quit")) {
      session.close();
      return;
    }

    Date date = new Date();
    session.write(date.toString());
    System.out.println("Message written...");
  }

  public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
    System.out.println("IDLE " + session.getIdleCount(status));
  }
}
